<?php
include "Backlog.php";

class BacklogDAO extends DataLayer {	 		
	/**
	* @desc Construtor da Classe
 	*/
	function __construct($lk){
		$this->lk = $lk;	
	}

	public function getBacklog($id=0, $idProject=0, $page=0){
	
		$this->sql ="	SELECT 
							SQL_CALC_FOUND_ROWS
							b.id_backlog_item, b.id_projeto, b.prioridade, b.prioridade_classe, b.nome, b.descricao, DATE_FORMAT(b.data_criacao, '%d/%m/%Y') data_criacao, b.id_usuario_criador, u.nome nameUserCreator,
							(select count(id_tarefa) from tarefas t where t.status <> 0 and t.id_backlog_item = b.id_backlog_item) tasks,
							(select count(id_tarefa) from tarefas t where t.status = 2 and t.id_backlog_item = b.id_backlog_item) tasksFinish
						FROM 
							backlog_itens b
						INNER JOIN
							usuarios u on u.id_usuario = b.id_usuario_criador
						WHERE 
							b.status <> 0";
		
		if($idProject > 0)
			$this->sql .= " and b.id_projeto = ".$idProject;

		if($id > 0)						
			$this->sql .= " and b.id_backlog_item = ".$id;
		
		$this->sql .= " ORDER BY b.id_backlog_item ASC";

		if($page > 0){
			$page = (int)$page;
			$page--;
			if($page < 0)
				$page = 0;
			else
				$page *= PAGINATION;

			$this->sql .= " LIMIT ".$page.",".PAGINATION;
		}
		
		$this->result = parent::select($this->sql);
		$backlogList = NULL;
		
		if(count($this->result) > 0){
			$i = 0;
			foreach($this->result as $row){
				$backlogList[$i] = new Backlog();				
				$backlogList[$i]->setDadosDB($row);				

				$i++;
			}
		}
		
		return $backlogList;
	}

	public function save(Backlog $obj){	
		if($obj != NULL){
			$dbObj = $obj->fieldsDBObj;
			unset($dbObj["id_backlog_item"]);
			if($obj->id > 0){
				parent::update("backlog_itens",$dbObj,"id_backlog_item = ".$obj->id);
				return $obj->id;
			}else{
				return parent::insert("backlog_itens",$dbObj,"data_criacao");
			}
		}else{
			return 0;
		}
	}

	public function getTaskBacklog($idProject){
		$this->sql ="	SELECT 
							b.id_backlog_item id, (select count(id_tarefa) from tarefas t where t.status <> 0 and t.id_backlog_item = b.id_backlog_item) tasks, (select count(id_tarefa) from tarefas t where t.status = 2 and t.id_backlog_item = b.id_backlog_item) tasksFinish 
						FROM 
							backlog_itens b
						WHERE 
							b.status <> 0 and b.id_projeto = ".$idProject;
		
		$this->result = parent::select($this->sql);

		return $this->result;
	}

	public function deleteBacklog($id){
		if($id > 0){
			$rsBacklog = $this->getBacklog($id);

			if(count($rsBacklog) > 0){
				if($rsBacklog[0]->tasks > 0){
					return -1;
				}else{
					$dados["status"] = 0;
					return parent::update("backlog_itens", $dados, "id_backlog_item=".$id);
				}				
			}
		}

		return 0;
	}
}


/*

for($i=3;$i<=25;$i++){
			$state = rand(0,3);

			if($state == 0){
				$priori = "BAIXA";
			}else if($state == 1 || $state == 2){
				$priori = "MÉDIA";
			}else{
				$priori = "ALTA";
			}

			$this->sql = "INSERT INTO backlog_itens (nome,prioridade,prioridade_classe,descricao,id_usuario_criador,id_projeto,status, data_criacao) VALUES ('Caso de Uso ".$i."','".$priori."','state".$state."','Caso de Uso ".$i."','1','1','1', now())";


			$this->result = parent::select($this->sql);
		}

		return $i;

		0------------------

		$count = 3;
		for($i=7;$i<=15;$i++){
			
			$this->sql = "INSERT INTO tarefas (id_backlog_item,id_sprint,ordem,nome,id_usuario_responsavel,id_tarefa_requisito,custo_estimado,data_inicio,data_limite,descricao,id_usuario_criador,id_tarefa_estado,status, data) VALUES (".$i.",'2','0','Caso de Uso ".$count."','7',NULL,'40','2011-06-27','2011-07-01','Caso de Uso ".$count."','1','1','1', now())";
			
			$this->result = parent::select($this->sql);
			$count++;
		}


		return $i;

*/
?>